package com.study.samsung.Dao;

import com.study.samsung.Entity.Classification;
import com.study.samsung.Utils.RowMapper;
import com.study.samsung.Utils.SQLHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class ClassificationDao {
    SQLHelper helper = new SQLHelper();

    public List<Classification> selectAll(){
        String sql = "select * from classification";
        return helper.query(sql,new ClassificationRowMapper());
    }

    public Integer changename(Integer lid,String lname){
        String sql = "update classification set lname = ? where lid = ?";
        return helper.update(sql,lname,lid);
    }

    public Integer addcategory(String lname,Integer xid){
        String sql = "insert into classification values(?,?,?)";
        return helper.insert(sql,null,lname,xid);
    }

    public Integer deladdcategory(Integer lid){
        String sql = "delete from classification where lid = ?";
        return helper.update(sql,lid);
    }

    class ClassificationRowMapper implements RowMapper<Classification>{

        @Override
        public Classification map(ResultSet rs) throws SQLException {
            return new Classification(rs.getInt("lid"),
                    rs.getString("lname"),
                    rs.getInt("xid"));
        }
    }
}
